﻿ 
#include "stdafx.h"
#include "user.h"
#include "export_functions.h"

  
//=========================================================================
//						 word_learn_level_e
//=========================================================================

bool word_learn_level_e::from_cstr(const char* str) throw (Error)
{
	static const size_t N = 32;

	if( strncmp(str, "none", N) == 0 )
	{
		value = none;
		return true;
	}

	if( strncmp(str, "low", N) == 0 )
	{
		value = low;
		return true;
	}

	if( strncmp(str, "aver", N) == 0 )
	{
		value = aver;
		return true;
	}

	if( strncmp(str, "hi", N) == 0 )
	{
		value = hi;
		return true;
	}

	if( strncmp(str, "perfect", N) == 0 )
	{
		value = perfect;
		return true;
	}
 

    String temp;
	temp.AssignF("Unknown source string value (%s)", str);
	throw Error(temp.c_str());
	return false;
}

void word_learn_level_e::to_cstr(char* dest) const    throw (Error)
{
	switch(value)
	{
	case none:
		{
			  sprintf(dest, "none");
		}
		break;

	case low:
		{
			sprintf(dest, "low");
		}
		break;

	case aver:
		{
			sprintf(dest, "aver");
		}
		break;

	case hi:
		{
			sprintf(dest, "hi");
		}
		break;

	case perfect:
		{
			sprintf(dest, "perfect");
		}
		break;



	default:
		{
			String temp;
			temp.AssignF("Unknown enum value (%u)", (unsigned)value  );
			throw Error(temp.c_str());
		}
	}

	
}











//=========================================================================
//						       UserInfo
//=========================================================================
UserInfo::UserInfo(const std::wstring& fullpath)  throw(Error)
{
	m_fullpath = fullpath;
	Load();
}

UserInfo::UserInfo(const std::wstring& directory, const std::string& user_name) throw(Error)
{
	{
		std::wstring temp = ksstr::convertAstringToWstringWinapi(user_name);
		temp += L".user";
		m_fullpath = directory + temp;

	}

	
	Load();
}

void UserInfo::Load()  throw(Error)
{
	CoreLogMessage("Need add code for load user info");
	if( ksfilefunc::fileExistsW( (LPCTSTR)m_fullpath.c_str()  ) )
	{
		// load from file

	}
	else
	{
		// not found
		Save();
	}

	//assert(false);
}

void UserInfo::Save() throw(Error)
{
	std::string data;
	
	assert(false && " need chaeck code ");

	// write to str
	{



		// write header
		{
			// write version
			
			
			// 

		}
		// end (write header)


		// write full info

		// write word info
		{
			for(size_t c=0; c<m_vector_user_learnword_info.size(); c++)
			{
			
			}



		}
		// end (write word info)


	}
	// end (write to str)



	if( !ksstr::saveStrToFileW( data, m_fullpath.c_str()) )
	{
		 throw Error("Ошибка сохранения UserInfo");
	}

	
}

std::string UserInfo::GetUserName() const
{
	WCHAR  dir[MAX_PATH];
	WCHAR  file[MAX_PATH];
	WCHAR  ext[MAX_PATH];
	ksfilefunc::filePathDecomposeW(dir, file, ext, m_fullpath.c_str() );
	std::string res = ksstr::convertWstringToAstringWinapi( std::wstring(file) );
	assert(res.length());
	return res;
}




//=========================================================================



static void  __cdecl  __FUNCFOUNDFILEW(const WCHAR* lpszFileName,  void* pUserData )
{
	std::vector<std::wstring>* foundFiles = (std::vector<std::wstring>*)pUserData;
	foundFiles->push_back(lpszFileName);

	int __end =0;
}

UserInfoList::UserInfoList(const std::wstring& directory)  throw(Error)
{
 

	m_list = new std::vector<UserInfo*>();

	 m_directory = directory;
	 if( !ksfilefunc::directoryExistW( (LPWSTR)m_directory.c_str() ) )
	 {
		 throw Error("Users directory not found !");
	 }

	 std::vector<std::wstring> foundFiles;
	 ksfilefunc::searchDirFilesW( directory.c_str(), L"*.user", __FUNCFOUNDFILEW, (void*)&foundFiles, FALSE );

	 if( foundFiles.size() == 0 )
	 {
		



	 }
	 else
	 {
		 std::string userfilename;

		 WCHAR strdir[MAX_PATH];
		 WCHAR strfile[MAX_PATH];
		 WCHAR strext [32];
		 for(size_t c=0; c<foundFiles.size(); c++ )
		 {
			 ksfilefunc::filePathDecomposeW(strdir, strfile, strext, foundFiles.at(c).c_str() );
			 std::string new_user = ksstr::convertWstringToAstringWinapi(strfile);
			 UserInfo* ui = AddUser( new_user );
		 }
		 

	 }

	// assert(false && "  NEED  load users ");
}

UserInfoList::~UserInfoList() 
{
	for(size_t c=0; c<m_list->size(); c++)
	{
		UserInfo* curr = m_list->at(c);
		if(curr)
		{
			delete curr;
			m_list->at(c) = NULL;
		}
	}

	delete m_list;

}

bool UserInfoList::IsUserExists(const std::string& user_name) const
{
	{
		// try
		std::wstring temp = this->m_directory;
	}

	if(m_list->size() == 0) return false;

	for(size_t c=0; c<m_list->size(); c++)
	{
		if( m_list->at(c)->GetUserName() == user_name )
		{
			return true;
		}
	}

	return false;
}


int UserInfoList::GetUserIndex(const std::string& user_name) const
{
	{
		// try
		std::wstring temp = this->m_directory;
	}

	for(size_t c=0; c<m_list->size(); c++)
	{
		if(m_list->at(c)->GetUserName() == user_name)
		{
			return (int)c;
		}
	}

	return -1;
}

UserInfo* UserInfoList::AddUser(const std::string& user_name)  throw(Error)
{
	if( IsUserExists(user_name) )
	{
		std::string msg;
		msg = "User " + user_name + " already exists !";
		throw Error(msg);
	}

	UserInfo* newuser = new UserInfo(m_directory,  user_name);
	m_list->push_back(newuser);
	return m_list->at(m_list->size()-1);

}

bool removeFile( std::string& fullPath )
{
	if(  ::DeleteFileA( (LPCSTR)fullPath.c_str() ) == 0)
	{
		return false;
	}

	return true;
}


bool removeFile( std::wstring& fullPath )
{
	if(  ::DeleteFileW( (LPCWSTR)fullPath.c_str() ) == 0)
	{
		return false;
	}

	return true;
}

void UserInfoList::DeleteUser(const std::string& username) throw(Error)
{


	for(size_t c=0; c<m_list->size(); c++)
	{
		if( m_list->at(c)->GetUserName() == username )
		{
		//	assert(false && "   ПРОВЕРИТЬ КОД УДАЛЕНИЯ ");

			// get file
			std::wstring fullpath = m_list->at(c)->GetFullPath();
			// remove from list
			UserInfo* ui = m_list->at(c);
			delete ui;
			m_list->erase( m_list->begin() + c );


			// remove  file
			if( !removeFile(fullpath) )
			{
				throw Error("Ошибка удаления файла пользователя");
			}


			return;
		}
	}

}

std::string UserInfoList::MakeUsersLine() const
{
	std::string res;
	for(size_t c=0; c<m_list->size(); c++)
	{
		res += m_list->at(c)->GetUserName();
		if(c != m_list->size()-1)
		{
			res += "\n";
		}
	}

	return res;
}

std::string UserInfoList::GetUserNameIndex(size_t index) throw(Error)
{
 
	if( index > (m_list->size()-1) )
	{
		std::string temp;
		temp += " ПОльзователя с индексом ";
		{
			char buf[32];
			sprintf( buf, "%i", (int)index );
			temp += buf;
		}
		temp += " не существует !";
		throw Error(  temp.c_str()  );
	}

	std::string res  = m_list->at(index)->GetUserName();
	return res;
}







